/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openjawtech.xscraper.scraper.util;

import java.util.Scanner;
import java.util.regex.MatchResult;
import com.openjawtech.xscraper.scraper.exception.ConfigurationException;

/**
 *
 * @Created on 16-Apr-2009, 00:50:26
 * @author Wenbo.Sun
 */
public class StringUtil {

    public static String[] stringToWords(String source) throws ConfigurationException {
        return source.trim().split("\\s+");
    }

    public static String simpleFormat(String source) throws ConfigurationException {
        return source.replaceAll("\\s+", " ").trim();
    }

    public static String getMatchResult(String source, String pattern) throws ConfigurationException {
        String dest = "";
        try {
            Scanner scanner = new Scanner(source);
            scanner.findInLine(pattern);

            MatchResult result = scanner.match();
//        dest = result.group(0);
            for (int i = 1; i <= result.groupCount(); i++) {
                dest += result.group(i);
            }
        } catch (Throwable t) {
        }
        return dest;
    }

    public static String[] commonSeparate(String src) {
        String source = src.trim();
        if (source.contains(";")) {
            return source.split(";");
        } else if (source.contains(" ")) {
            return source.split(" ");
        } else if (source.contains(",")) {
            return source.split(",");
        } else {
            return new String[]{src};
        }
    }
}
